<?php

namespace Admin\Controller;
use Think\Controller;

class FunctionController extends AdminBasicController{

	public $Module = '';
	public $Function = '';
	public $Project = '';

	public function _initialize(){
		$this->checkLogin();
		$this->Module = D('Module');
		$this->Function = D('Function');
		$this->Project = D('Project');
	}

	/**
	 * 方法列表
	 */
	public function FunctionList(){
		$where = array();
		if(isset($_POST['f_name'])){
			$where['f_name'] = array('like','%'.$_POST['f_name'].'%');
		}
		$function_list = $this->Function->selectFunction($where,'ctime desc',10);
		//获取分页
		$page = $function_list['page'];
		//获取分页数据
		$function_list = $function_list['list'];
		//获取所属模块名称
		foreach ($function_list as $k => $v) {
			$where['mo_id'] = $v['mo_id'];
			$function_list[$k]['mo_name'] = $this->Module->findModule($where);
		}
		$this->assign('function_list',$function_list);
		$this->assign('page',$page);
		$this->display('Function/functionList');
	}

	public function ajaxModule(){
		$where['p_id'] = $_POST['pid'];
		$module_list = $this->Module->selectModule($where,'','');
		$this->ajaxReturn($module_list,'JSON');
	}

	public function ajaxFun(){
		$where['mo_id'] = $_POST['moid'];
		$function_list = $this->Function->selectFunction($where,'','');
		$this->ajaxReturn($function_list,'JSON');
	}

	/**
	 * 增加方法
	 */
	public function addFunction(){
		if(empty($_POST)){
			$project_list = $this->Project->selectProject('','ctime desc','');
			$this->assign('project_list',$project_list);
			$this->display('Function/addFunction');
		}else{
			if($data=$this->Function->create()){
				$data['ctime'] = time();
				if($this->Function->addFunction($data)){
					$this->success('添加成功',U('Function/functionList'));
				}else{
					$this->success('添加失败');
				}
			}else{
				$this->error($this->Function->getError());
			}
		}
	}

	/**
	 * 编辑方法
	 */
	public function editFunction(){
		if(empty($_POST)){
			$where['f_id'] = I('get.f_id');
			$function_find = $this->Function->findFunction($where);
			$project_list = $this->Project->selectProject('','ctime desc','');
			$this->assign('project_list',$project_list);
			$this->assign('function_find',$function_find);
			$this->display('Function/editFunction');
		}else{
			if($data=$this->Function->create()){
				if($this->Function->editFunction(array('f_id'=>$_POST['f_id']),$data)){
					$this->success('编辑成功',U('Function/functionList'));
				}else{
					$this->success('编辑失败');
				}
			}else{
				$this->error($this->Function->getError());
			}
		}
	}

	/**
	 * 删除方法
	 */

	public function deleteFunction(){
		$where['f_id'] = $_GET['f_id'];
		$id = $this->Function->deleteFunction($where);
		if($id){
			$this->success('删除成功');
		}else{
			$this->success('删除失败');
		}
	}


}


?>